package spitfire;

import light3d.*;

import java.awt.*;

public class SpitfireLandscape extends SpitfireBufferedCanvas
{
  final int HOUSE_COUNT = 30;
  final int MESH_NUM = 1;
  final double MAX_WIDTH = 1000.0f, MAX_LENGTH = 1000.0f, TREE_START = 200.0f;

  double treeX[], treeZ[], treeR[];
  int treeType[];
  WavefrontMesh[] treeMesh;
  BasicMesh scape;
  SpitfireBase baseB, baseR;
 
  public SpitfireLandscape()
  {
    super();
    baseB = new SpitfireBase(0);
    baseR = new SpitfireBase(1);

    treeType = new int[HOUSE_COUNT];
    treeX = new double[HOUSE_COUNT];
    treeZ = new double[HOUSE_COUNT];
    treeR = new double[HOUSE_COUNT];

    treeMesh = new WavefrontMesh[HOUSE_COUNT];

    for (int i = 1; i <= MESH_NUM; i++)
      treeMesh[i - 1] = new WavefrontMesh("data/tree" + i + ".obj");

    for (int i = 0; i < HOUSE_COUNT; i++)
    {
      treeX[i] = Math.random() * MAX_WIDTH - MAX_WIDTH/2.0;
      treeZ[i] = Math.random() * TREE_START - TREE_START/2.0;
      treeR[i] = Math.random() * 360;
      
      treeType[i] = (int)(Math.random() * (MESH_NUM - 1));
    }
    scape = new BasicMesh();
    Triangle[] t = new Triangle[2];
    t[0] = new Triangle(BufferedCanvas.getRGBColor(155, 100, 100),
                        -MAX_WIDTH/2.0, 0, -MAX_LENGTH/2.0,
                        MAX_WIDTH/2.0, 0, -MAX_LENGTH/2.0,
                        -MAX_WIDTH/2.0, 0, MAX_LENGTH/2.0);
    t[1] = new Triangle(BufferedCanvas.getRGBColor(155, 100, 100),
                        -MAX_WIDTH/2.0, 0, MAX_LENGTH/2.0,
                        MAX_WIDTH/2.0, 0, MAX_LENGTH/2.0,
                        MAX_WIDTH/2.0, 0, -MAX_LENGTH/2.0);
    scape.appendTriangles(t);
    scape.finalizeMesh();

  }
	public double endRender(BufferedCanvas bufferedCanvas, PointWithAngle camera)
	{
    for (int i = 0; i < HOUSE_COUNT; i++)
    {
      treeMesh[treeType[i]].applyColor(BufferedCanvas.getRGBColor(100, 200, 100));
      treeMesh[treeType[i]].setPosition(treeX[i], 0, treeZ[i]);
      treeMesh[treeType[i]].rotate(0, treeR[i], 0);
      treeMesh[treeType[i]].render(bufferedCanvas, camera);
      treeMesh[treeType[i]].resetChanges();
    }

		specks.render(bufferedCanvas, camera);
    scape.render(bufferedCanvas, camera);
    
    baseB.render(bufferedCanvas, camera);
    baseR.render(bufferedCanvas, camera);

		return endRender();
	}
}
